/**
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */

package com.poltman.dscentral.system;


import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;

import com.poltman.dscentral.core.CoreStatics;
import com.poltman.dscentral.core.webappinit.WebAppInit;

/**
 * 
 * @author Zbigniew Ciok; z.ciok@poltman.com
 * @version $Revision: 1670 $
 *
 */

@SuppressWarnings("rawtypes")
public class AuthenticationWinController extends GenericForwardComposer {
	
	private static final long serialVersionUID = -1207988396036946506L;
	
	private static Logger log = Logger.getLogger(AuthenticationWinController.class);
	
	private Listbox authenticationTargetListbox;
	private Listitem ipLitem;
	private Listitem ldapLitem;
	private Listitem hLdapLitem;
	private Listitem passwordLitem;
	private Listitem shibbolethLitem;
	private Listitem x509Litem;
	
	private SortedMap<Integer, String> map = new TreeMap<Integer, String>();
	
	@SuppressWarnings("unchecked")
	@Override
    public void doAfterCompose(org.zkoss.zk.ui.Component comp) throws Exception {
    	super.doAfterCompose(comp);
    	authenticationListsSetup();
    	for (int i = 1; i <= 6; i++) rightItemSelection(i);
    }
	
	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("unused")
	private void authenticationListsSetup() {
		String dspaceAuthenticationFilePath = null;
		String authenticationMethod = null;
		try {
			dspaceAuthenticationFilePath = 
				WebAppInit.getInstance().getDspaceDir() + File.separator + "config" + 
				File.separator + "modules" + 
				File.separator + "authentication.cfg";
			
			Properties pro = new Properties();		
			FileInputStream in = new FileInputStream(dspaceAuthenticationFilePath);
			pro.load(in);
			authenticationMethod = pro.getProperty("plugin.sequence.org.dspace.authenticate.AuthenticationMethod");
			//Test null:
			authenticationMethod.equals("");
		} catch (Exception e) {
			return;
		}
		
		if (authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_PASSWORDAUTHENTICATION) > -1) {
			map.put(authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_PASSWORDAUTHENTICATION), CoreStatics.PASSWORD);
			passwordLitem.setVisible(false);
		} 
		if (authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_SHIBAUTHENTICATION) > -1) {
			map.put(authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_SHIBAUTHENTICATION), CoreStatics.SHIBBOLETH);
			shibbolethLitem.setVisible(false);
		} 
		if (authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_LDAPHIERARCHICALAUTHENTICATION) > -1) {
			map.put(authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_LDAPHIERARCHICALAUTHENTICATION), CoreStatics.HLDAP);
			hLdapLitem.setVisible(false);
		} 
		if (authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_LDAPAUTHENTICATION) > -1) {
			map.put(authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_LDAPAUTHENTICATION), CoreStatics.LDAP);
			ldapLitem.setVisible(false);
		} 
		if (authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_IPAUTHENTICATION) > -1) {
			map.put(authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_IPAUTHENTICATION), CoreStatics.IP);
			ipLitem.setVisible(false);
		} 
		if (authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_X509AUTHENTICATION) > -1) {
			map.put(authenticationMethod.indexOf(CoreStatics.ORG_DSPACE_AUTHENTICATE_X509AUTHENTICATION), CoreStatics.X509);
			x509Litem.setVisible(false);
		} 
	}
	
	/**
	 * 
	 * @param kind
	 */
	private void rightItem(String kind) {
		Listitem listitem = new Listitem();
		Listcell listcell = new Listcell();    	
		listitem.setDraggable("true");
		listitem.setDroppable("true");
		listitem.setVisible(true);
		listcell.setLabel(kind);
		listitem.appendChild(listcell);
		authenticationTargetListbox.appendChild(listitem);
	}	
	
	/**
	 * 
	 * @param itemNumber
	 */
	private void rightItemSelection(int itemNumber) {
		if (map.size() >= itemNumber) {
			if (mapValueOf(itemNumber) == CoreStatics.IP) {
				rightItem(CoreStatics.IP);
			} else if (mapValueOf(itemNumber) == CoreStatics.LDAP) {
				rightItem(CoreStatics.LDAP);
			} else if (mapValueOf(itemNumber) == CoreStatics.HLDAP) {
				rightItem(CoreStatics.HLDAP);
			} else if (mapValueOf(itemNumber) == CoreStatics.PASSWORD) {
				rightItem(CoreStatics.PASSWORD);
			} else if (mapValueOf(itemNumber) == CoreStatics.SHIBBOLETH) {
				rightItem(CoreStatics.SHIBBOLETH);
			} else if (mapValueOf(itemNumber) == CoreStatics.X509) {
				rightItem(CoreStatics.X509);
			}
		}	
	}

	/**
	 * 
	 */
	private String mapValueOf(int ind) {
		int aInd = 0;
		String value = null;
		Iterator iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			Integer key = (Integer) iterator.next();
			value = map.get(key);
			aInd++;
			if (aInd >= ind) break;
		}
		return value;
	}	
	
	/**
	 * refresh
	 */
	//TODO: refresh
	public void onClick$refreshAuthenticationStackBtn() {
//    	authenticationListsSetup();
//    	for (int i = 1; i <= 6; i++) rightItemSelection(i);
	}	
}
